import { request } from '@/utils/request';

// 后端基础URL - 使用相对路径，通过Vite代理
const BASE_URL = '';

/**
 * 上传身份证正面照片
 */
export const uploadIdCardFront = (filePath: string) => {
  console.log('上传身份证正面，文件路径:', filePath);
  console.log('请求URL:', `${BASE_URL}/drone/photo/upload/idcard/front`);
  
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `/drone/photo/upload/idcard/front`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("officialAccount_token") || ''
      },
      success: (res: any) => {
        console.log('身份证正面上传响应:', res);
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.msg);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('响应解析失败'));
        }
      },
      fail: (err: any) => {
        console.error('身份证正面上传失败:', err);
        reject(new Error(err.errMsg || '上传失败'));
      }
    });
  });
};

/**
 * 上传身份证反面照片
 */
export const uploadIdCardBack = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `/drone/photo/upload/idcard/back`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("officialAccount_token") || ''
      },
      success: (res: any) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.msg);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('响应解析失败'));
        }
      },
      fail: (err: any) => {
        reject(new Error(err.errMsg || '上传失败'));
      }
    });
  });
};

/**
 * 上传探险信息照片
 */
export const uploadExploration = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `/drone/photo/upload/exploration`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("officialAccount_token") || ''
      },
      success: (res: any) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.msg);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('响应解析失败'));
        }
      },
      fail: (err: any) => {
        reject(new Error(err.errMsg || '上传失败'));
      }
    });
  });
};

/**
 * 上传个人资质照片
 */
export const uploadQualification = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `/drone/photo/upload/qualification`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("officialAccount_token") || ''
      },
      success: (res: any) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.msg);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('响应解析失败'));
        }
      },
      fail: (err: any) => {
        reject(new Error(err.errMsg || '上传失败'));
      }
    });
  });
};

/**
 * 上传证书照片
 */
export const uploadCertificate = (filePath: string) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `${BASE_URL}/drone/photo/upload/certificate`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("officialAccount_token") || ''
      },
      success: (res: any) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data.msg);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('响应解析失败'));
        }
      },
      fail: (err: any) => {
        reject(new Error(err.errMsg || '上传失败'));
      }
    });
  });
};

/**
 * 上传技能证明照片
 */
export const uploadSkill = (filePath: string) => {
  console.log('上传技能证明，文件路径:', filePath);
  console.log('请求URL:', `${BASE_URL}/drone/photo/upload/skill`);
  
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: `/drone/photo/upload/skill`,
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync("token") || uni.getStorageSync("Authorization") || ''
      },
      success: (res: any) => {
        console.log('技能证明上传成功:', res);
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data);
          } else {
            reject(data);
          }
        } catch (e) {
          reject({ msg: '响应数据解析失败' });
        }
      },
      fail: (err: any) => {
        console.error('技能证明上传失败:', err);
        reject(err);
      }
    });
  });
};